package SGEngine2

import (
	"github.com/gl"
)

type Camera struct {
	CameraNode *Node
	Near       float64
	Far        float64
	BGColor    Color3
}

func (c *Camera) Init() {
	if c.Near == 0 {
		c.Near = -10
	}
	if c.Far == 0 {
		c.Far = 10
	}
}

func (c *Camera) VisitNodes(node *Node, pmat *Mat4) {
	if node.Active() {
		node.Update()
	}
	node.LocalToWorld = pmat
	if node.render != nil && c.IsInCamera(node) {
		RenderCanvas.Add(node)
	}

	if node.childNodes != nil {
		for i := 0; i < node.childNodes.Count(); i++ {
			mat := node.GetWorldMat()
			c.VisitNodes(node.childNodes.Get(i).(*Node), mat)
		}
	}

}

func (c *Camera) IsInCamera(node *Node) bool {
	return true
}

func (c *Camera) SetCamera() {
	gl.MatrixMode(gl.PROJECTION)
	gl.LoadIdentity()
	gl.Ortho(float64(-RootView.Width/2), float64(RootView.Width/2), float64(-RootView.Height/2), float64(RootView.Height/2), c.Near, c.Far)
	gl.Enable(gl.DEPTH_TEST)
	gl.DepthFunc(gl.LEQUAL)
	gl.ClearColor(gl.GLclampf(c.BGColor.R), gl.GLclampf(c.BGColor.G), gl.GLclampf(c.BGColor.B), 1)
	gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
	gl.ClearDepth(1)
}
